//
//  ActionHandler.m
//  MVP_DesignModel_OCVersion
//
//  Created by DeLong Yang on 2017/7/20.
//  Copyright © 2017年 DeLongYang. All rights reserved.
//

#import "ActionHandler.h"
#import "TemplateAction.h"

@implementation ActionHandler
{
    NSInvocation *invocation_;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        invocation_ = [[NSInvocation alloc] init];
        [invocation_ setTarget:self];
    }
    return self;
}

- (BOOL)handleAction:(TemplateAction *)action
{
    if (!action) {
        return NO;
    }
    
    _action = action;
    __weak typeof (self) weakSelf  = self;
    
    // 开始任务前的调用
    if (self.delegate&&[self.delegate respondsToSelector:@selector(actionHandler:willHandleAction:)]) {
        [self.delegate actionHandler:self willHandleAction:action];
    }
    
    // 确实完成任务的回调
    [action performTaskWithCompletionBlock:^(){
        if (weakSelf.delegate&&[weakSelf.delegate respondsToSelector:@selector(actionHandler:didHanlderAction:)]) {
            [weakSelf.delegate actionHandler:weakSelf didHanlderAction:action];
        }

    }];
    
    return YES;
    
}


@end
